package com.isyscore.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isyscore.dto.PageInfoDto;
import com.isyscore.entity.admin.SysPost;
import com.isyscore.exception.FjMesException;
import com.isyscore.form.sys.post.ListPostForm;
import com.isyscore.mapper.admin.SysPostMapper;
import com.isyscore.service.admin.SysPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author LGL
 * @since 2024年10月25日
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Override
    public IPage<SysPost> listPost(ListPostForm form) {
        QueryWrapper<SysPost> queryWrapper = new QueryWrapper<>();
        if(form.getPostCode()!=null && !"".equals(form.getPostCode())){
            queryWrapper.like("post_code",form.getPostCode());
        }
        if(form.getPostName()!=null && !"".equals(form.getPostName())){
            queryWrapper.like("post_name",form.getPostName());
        }
        if(form.getStatus()!=null && !"".equals(form.getStatus())){
            queryWrapper.eq("status",form.getStatus());
        }
        queryWrapper.orderByAsc("post_sort");
        return baseMapper.selectPage(new Page<>(form.getPageNum(), form.getPageSize()), queryWrapper);
    }

    @Override
    public boolean add(SysPost post) {
        //判断部门 编码和名称是否已经存在数据库
        SysPost sysPost = baseMapper.selectOne(new QueryWrapper<SysPost>().eq("post_code", post.getPostCode()).or().eq("post_name", post.getPostName()));
        if(sysPost!=null){
            throw new FjMesException(400, "部门编码或名称已存在");
        }
        post.setCreateTime(LocalDateTime.now());
        return baseMapper.insert(post)>0;
    }

    @Override
    public boolean edit(SysPost post) {
        //判断部门 编码和名称是否已经存在数据库,但要排除当前用户
        SysPost sysPost = baseMapper.selectOne(new QueryWrapper<SysPost>().ne("post_id", post.getPostId()).and(i->i.eq("post_code", post.getPostCode()).or().eq("post_name", post.getPostName())));
        if(sysPost!=null){
            throw new FjMesException(400, "部门编码或名称已存在");
        }
        post.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(post)>0;
    }
}
